package com.epro.dx.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epro.dx.R;
import com.weipu.common.constants.Constant;
import com.weipu.common.observer.RedDotObserver;
import com.weipu.common.subject.RedDotSubjectImpl;
import com.weipu.common.util.utils.SPUtils;

/**
 * 底部功能按钮控件
 * @author huangfei
 *
 */
public class BottomBtn extends LinearLayout implements OnClickListener, RedDotObserver {
	public static final String HOME_TAG = "Home";
	public static final String BROWSE_TAG = "Category";
	public static final String SHOPPING_TAG = "Cart";
	public static final String ACCOUNT_TAG = "My Account";
	private String currentTab=HOME_TAG;
	private LinearLayout homeBtn, browseBtn, shoppingCartBtn;

	private RelativeLayout settingsBtn;

	private ImageView homeImage, browseImage, shoppingCartImage, settingImage;

	private TextView homeTv, browseTv, shoppingCartTv, settingTv, cartNum;

	private OnBtnClickListenner listenner;

	private int currentIndex = -1;

	private TextView[] textViews;

	private ImageView[] imageViews;

	//My DK 新功能小红点
	private ImageView settingsRedIv;
	private boolean isKnowNewFun;
	private SPUtils spUtils;

	public BottomBtn(Context context) {
		super(context);
	}

	public BottomBtn(Context context, AttributeSet attrs) {
		super(context, attrs);
		View view = LayoutInflater.from(context).inflate(
				R.layout.bottom_btn_layout, null);
		// addView(view);
		addView(view, new LayoutParams(LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT));
		intView(view);
		setListenner();

		//注册小红点观察者 让这个控件成为观察者
		RedDotSubjectImpl.getInstance().registerObserver(this);

		//从sp当中获取是否已经点击过新功能，如果为true，则不显示小红点，
		//如果为false，显示小红点
		spUtils = new SPUtils(getResources().getString(R.string.app_name_en));
		//是否已经阅读过新功能
		isKnowNewFun = spUtils.getBoolean(Constant.Global.IS_KONW_NEW_FUN, false);
		if (!isKnowNewFun) {
			settingsRedIv.setVisibility(View.VISIBLE);
//					spUtils.put(Constant.Global.IS_KONW_NEW_FUN,true);
		}
		initData();
	}

	private void intView(View view) {
		homeBtn = (LinearLayout) view.findViewById(R.id.home_btn);
		homeBtn.setTag(0);
		browseBtn = (LinearLayout) view.findViewById(R.id.browse_btn);
		browseBtn.setTag(1);
		shoppingCartBtn = (LinearLayout) view
				.findViewById(R.id.shopping_cart_btn);
		shoppingCartBtn.setTag(2);
		settingsBtn = (RelativeLayout) view.findViewById(R.id.settings_btn);
		settingsBtn.setTag(3);

		homeImage = (ImageView) view.findViewById(R.id.home_item_iv);
		browseImage = (ImageView) view.findViewById(R.id.browse_item_iv);
		shoppingCartImage = (ImageView) view
				.findViewById(R.id.shopping_cart_item_iv);
		settingImage = (ImageView) view.findViewById(R.id.settings_item_iv);

		homeTv = (TextView) view.findViewById(R.id.home_item_tv);
		browseTv = (TextView) view.findViewById(R.id.browse_item_tv);
		shoppingCartTv = (TextView) view
				.findViewById(R.id.shopping_cart_item_tv);
		settingTv = (TextView) view.findViewById(R.id.settings_item_tv);
		cartNum = (TextView) view.findViewById(R.id.tv_toast);
		settingsRedIv = (ImageView) view.findViewById(R.id.settings_item_red_tv);

		textViews = new TextView[] { homeTv, browseTv, shoppingCartTv,
				settingTv };
		imageViews = new ImageView[] { homeImage, browseImage,
				shoppingCartImage, settingImage };
	}

	private void initData() {
		if (isKnowNewFun) {
			settingsRedIv.setVisibility(View.GONE);
		}
	}

	private void setListenner() {
		homeBtn.setOnClickListener(this);
		browseBtn.setOnClickListener(this);
		shoppingCartBtn.setOnClickListener(this);
		settingsBtn.setOnClickListener(this);
	}

	/**
	 * 设置当前显示
	 * 
	 * @param tag
	 */
	public void setCurrentTab(String tag) {
		if (HOME_TAG.equals(tag)) {
			onClick(homeBtn);
		} else if (BROWSE_TAG.equals(tag)) {
			onClick(browseBtn);
		} else if (SHOPPING_TAG.equals(tag)) {
			onClick(shoppingCartBtn);
		} else if (ACCOUNT_TAG.equals(tag)) {
			onClick(settingsBtn);
		}
	}

	/**
	 * 获取当前选中的底部按钮
	 * @return
	 */
	public String getCurrentTab() {
		return currentTab;
	}

	@Override
	public void onClick(View view) {
//		setViewStatus((Integer) view.getTag());
		switch (view.getId()) {
		case R.id.home_btn:
			if (!HOME_TAG.equals(currentTab)&&null!=listenner) {
				setViewStatus((Integer) view.getTag());
				currentTab = HOME_TAG;
				listenner.setCurrentFragment(0);
			}
			break;
		case R.id.browse_btn:
			if (!BROWSE_TAG.equals(currentTab)&&null!=listenner) {
				setViewStatus((Integer) view.getTag());
				currentTab = BROWSE_TAG;
				listenner.setCurrentFragment(1);
			}
			break;
		case R.id.shopping_cart_btn:
			if (!SHOPPING_TAG.equals(currentTab)&&null!=listenner) {
				setViewStatus((Integer) view.getTag());
				currentTab = SHOPPING_TAG;
				listenner.setCurrentFragment(2);
			}
			break;
		case R.id.settings_btn:
			if (!ACCOUNT_TAG.equals(currentTab)&&null!=listenner) {
				setViewStatus((Integer) view.getTag());
				currentTab = ACCOUNT_TAG;
				listenner.setCurrentFragment(3);
			}
			break;

		default:
			break;
		}
		
	}
	
	public void accountShow(){
		imageViews[3].setBackgroundResource(R.drawable.dx_on);
		currentTab = ACCOUNT_TAG;
		listenner.setCurrentFragment(3);
	}

	/**
	 * 设置shopping_Cart值
	 * @param num
	 */
	public void setToastNum(int num) {
		if(null==cartNum){
			return;
		}
		if (num > 0) {
			cartNum.setVisibility(View.VISIBLE);
			// 大于99 显示99+
			if (num > 99) {
				cartNum.setText("99+");
			} else {
				cartNum.setText(String.valueOf(num));
			}
		} else {
			cartNum.setVisibility(View.INVISIBLE);
		}
	}

	/**
	 * 修改被选择Btn的状态
	 * @param which Btn Index
	 */
	public void setViewStatus(int which) {
		switch (which) {
		case 0:
			imageViews[0].setBackgroundResource(R.drawable.home_on);
			break;
		case 1:
			imageViews[1].setBackgroundResource(R.drawable.browse_on);
			break;
		case 2:
			imageViews[2].setBackgroundResource(R.drawable.shoppingcart_on);
			break;
		case 3:
			imageViews[3].setBackgroundResource(R.drawable.dx_on);
			break;
		default:
			break;
		}
		// 恢复前一个按钮的状态
		switch (currentIndex) {
		case 0:
			imageViews[0].setBackgroundResource(R.drawable.home);
			break;
		case 1:
			imageViews[1].setBackgroundResource(R.drawable.browse);
			break;
		case 2:
			imageViews[2].setBackgroundResource(R.drawable.shoppingcart);
			break;
		case 3:
			imageViews[3].setBackgroundResource(R.drawable.dx);
			break;
		default:
			break;
		}
		if (currentIndex != -1) {
			textViews[currentIndex].setTextColor(getResources().getColor(
					R.color.btn_unselected));
		}
		textViews[which].setTextColor(getResources().getColor(
				R.color.btn_selected));
		currentIndex = which;
	}

	/**
	 * 当被观察者通知消息已读时
	 */
	@Override
	public void noticeHasRead() {
		settingsRedIv.setVisibility(View.GONE);
	}


	/**
	 * Btn点击响应事件
	 * @author Administrator
	 *
	 */
	public interface OnBtnClickListenner {
		void setCurrentFragment(int which);
	}

	/**
	 * 注册Btn点击事件监听
	 * @param listenner
	 */
	public void setBtnClickListenner(OnBtnClickListenner listenner) {
		this.listenner = listenner;
	}

}
